/**
 * Created by CSJ11 on 2017/7/3.
 */
'use strict';
angular.module('app')
    .controller('bannerController', [ '$rootScope', '$scope', '$http', '$state','$timeout',
        function($rootScope, $scope, $http, $state, $timeout) {
          // alert(  $("#account").html());
            $scope.title = '轮播图列表';
            $scope.searchParams = { };
            $scope.loading = false;
            $scope.param={};
            $scope.search = function () {
                $scope.loading = true;
                $.ajax({
                    url : $scope.app.project_name+'/platform/banner/list',
                    data: $scope.searchParams
                }).then(function(result) {
                    console.log(result);
                    $scope.loading = false;
                    if (result.httpCode == 200) {

                        $scope.pageInfo = result.data.list;
                        if(result.data.sRole[0].userType=='1'){
                            $("#admin").show();
                            $scope.platformSource=result.data.sRole[0].userType;
                        }else{
                            $("#admin").hide();
                            $scope.platformSource=result.data.sRole[0].userType;
                        }

                    } else {
                        $scope.msg = result.msg;
                    }
                    $scope.$apply();
                });
            }



            $scope.searchRe = function () {
                $scope.loading = true;
                $scope.searchParams.pageNum = 1;
                $.ajax({
                    url : $scope.app.project_name+'/platform/banner/list',
                    data: $scope.searchParams
                }).then(function(result) {

                    $scope.loading = false;
                    if (result.httpCode == 200) {
                        $scope.pageInfo = result.data.list;
                        if(result.data.sRole[0].userType=='1'){
                            $("#admin").show();
                            $scope.platformSource=result.data.sRole[0].userType;
                        }else{
                            $("#admin").hidden;
                            $scope.platformSource=result.data.sRole[0].userType;
                        }
                        $(".editSortBtn").show();
                        $(".saveSortBtn").hide();
                    } else {
                        $scope.msg = result.msg;
                    }
                    $scope.$apply();
                });
            }

            $scope.selected = [];
            var updateSelected = function (action, contact) {
                if (action == 'add' && $scope.selected.indexOf(contact.id) == -1){
                    $scope.selected.push(contact.id);
                    contact.checked = true;
                }

                if (action == 'remove' && $scope.selected.indexOf(contact.id) != -1){
                    $scope.selected.splice($scope.selected.indexOf(contact.id), 1);
                    contact.checked = false;
                }
            };

            $scope.updateSelection = function ($event, contact) {
                var checkbox = $event.target;
                var action = (checkbox.checked ? 'add' : 'remove');
                if (action == 'add' && $scope.selected.indexOf(contact.id) == -1){
                    $scope.selected.push(contact.id);
                    contact.checked = true;
                }
                if (action == 'remove' && $scope.selected.indexOf(contact.id) != -1){
                    $scope.selected.splice($scope.selected.indexOf(contact.id), 1);
                    contact.checked = false;
                }
            };
            // 全选操作
            $scope.selectAll = function ($event) {
                var checkbox = $event.target;
                var action = (checkbox.checked ? 'add' : 'remove');
                for (var i = 0; i < $scope.pageInfo.records.length; i++) {
                    var contact = $scope.pageInfo.records[i];

                    updateSelected(action, contact);
                }
                console.log(contact)
            };

            // 编辑排序
            $scope.editSort=function ($event) {
                if($('#banEnable').val()=='N'){
                    $scope.alertModal({'content':'非可用状态不能编辑排序'});
                    return;
                }
                if(!$('#banEnable').val()){
                    $scope.alertModal({'content':'状态不能为空'});
                    return;
                }
                if(!$('#type').val()){
                    $scope.alertModal({'content':'类型不能为空'});
                    return;
                }
                // if(!$('#platformSource').val()){
                //     $scope.alertModal({'content':'平台来源不能为空'});
                //     return;
                // }
                if($('#bannerName').val()){
                    $scope.alertModal({'content':'名称为空方可编辑排序'});
                    return;
                }
                $("#bannerList").sortable({opacity: 0.5,//拖动的透明度
                    revert: true, //缓冲效果
                    cursor: 'move', //拖动的时候鼠标样式
                    connectWith: ".column", scrollSpeed: 40});
                $("#bannerList").disableSelection();

                for (var i = 0; i < $scope.pageInfo.records.length; i++) {
                    var tr = $(".sort  tbody tr")[i];
                    $($(tr).find("td")[5]).hide();
                    $($(tr).find("td")[6]).hide();
                    $(".editSortBtn").hide();
                    $("#sortId").hide();
                    $(".saveSortBtn").show();
                }
            }
            $scope.saveSort=function ($event) {
                var batchUpdates = new Array();//声明一个数组来保存多个的id值
                var sortArray = new Array();//声明一个数组来保存多个序号的值
                for (var j = 0; j < $scope.pageInfo.records.length; j++) {

                    var tr = $(".sort  tbody tr")[j];

                    var id = $($(tr).find("td")[1]).html();
                    var sort = j+1;
                    sortArray[j]=sort;//用来存放序号
                    var batchUpdate = id + "-" + sort;
                    batchUpdates[j] = batchUpdate;
                    // if (!isNaN(sort) && sort > 0 && sort <= $scope.pageInfo.records.length) {
                    //     continue;
                    // } else {
                    //     $scope.alertModal({"content": "数据格式输入有误或序号大于了现有的最大的条数"});
                    //     flag = false;
                    // }
                }
                // var s = sortArray;//用来存放序号的值
                // var count=0;
                // for(var m=0;m<s.length;m++) {//验证是否有重复的。如果有的话就加一
                //     if (s[m] == s[m + 1]) {
                //         count++;
                //     }
                // }
                // if(count!=0){
                //     $scope.alertModal({"content": "序号重复，请重新排序"});
                //     flag = false;
                // }
                    $.ajax({
                        url: $scope.app.project_name + '/platform/banner/editSort',
                        data: {'ids': batchUpdates},
                        traditional: true
                    }).then(function (result) {
                        console.log(result);
                        if (result.httpCode == 200) {
                            $scope.alertModal({"content": "操作成功"});
                            $(".editSortBtn").show();
                            $("#sortId").show();
                            $(".saveSortBtn").hide();
                            $("#bannerList").sortable('destroy');//解除拖動
                            $timeout(function () {
                                $scope.search();
                            }, 1000);
                        } else {
                            $scope.msg = result.msg;
                        }
                    });

            }
            $scope.isSelected = function (id) {
                return $scope.selected.indexOf(id) >= 0;
            };


            $scope.search();

            $scope.clearSearch = function() {

                $scope.searchParams= { };
                $scope.search();
                $(".editSortBtn").show();
                $(".saveSortBtn").hide();
            }



            // 翻页
            $scope.pagination = function (pageNo) {
                $scope.searchParams.pageNum=pageNo;
                $scope.search();
            };


            $scope.businessLicenses = [];
            $scope.preview = function(businessLicense){
                if (businessLicense != null && businessLicense != '') {
                    $scope.businessLicenses = businessLicense.split('|');
                }
                $scope.firstApply = true;
            }

            $scope.oper = function (id,$event) {

                $.ajax({
                    url : $scope.app.project_name+'/platform/banner/banEnable',
                    data: {id:id,banEnable:$event.target.checked?'Y':'N',state:$event.target.checked?'N':'Y'}
                }).then(function(result) {
                    $scope.loading = false;
                    $scope.alertModal({"content":"操作成功"});
                    if (result.httpCode != 200) {
                        $scope.alertModal(result.msg);
                    }
                    $scope.search();
                });
            }
            // 删除轮播图
            $scope.deleteById=function (id) {
                $scope.alertModel({
                    text:'确定删除？',
                    btnCancel:true,
                    resCode:"10000"
                }).then(function(){
                    $.ajax({
                        url : $scope.app.project_name+'/platform/banner/deleteById',
                        data: {id:id}
                    }).then(function(result) {
                        $scope.loading = false;
                        $scope.alertModal({"content":"操作成功"});
                        if (result.httpCode != 200) {
                            $scope.alertModal(result.msg);
                        }
                        $scope.search();
                    });

                })

            }

        } ]);